package com.qf.web.controller;

import com.alibaba.fastjson.JSON;
import com.qf.dao.Showselect;
import com.qf.dao.impl.ShowselectImpl;
import com.qf.domain.*;
import com.qf.service.*;
import com.qf.service.impl.*;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * projectName:xiaomi1
 * author:
 * time:2024/4/19 20:55
 * description:
 */
@WebServlet("/showGoodservlet")
public class ShowGoodServlet extends BaseServlet {
    //获取会员用户
    public String getUserList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        try {
            ShowGoodService showGoodservice = new ShowGoodServiceImpl();
            List<User> userList = showGoodservice.select();
            //存储数据
            String list = JSON.toJSONString(userList);
            response.getWriter().write(list);
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询会员用户信息失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    //获取商品分类
    public String getshowGoodsType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        try {
            //2调用业务逻辑
            GoodsService goodsService = new GoodsServiceImpl();
            List<GoodsType> goodsTypeList1 = goodsService.find();
            //存储数据
            String goodsTypeList = JSON.toJSONString(goodsTypeList1);
            response.getWriter().write(goodsTypeList);
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "预览订单失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    //获取失效会员用户
    public String getinvalidUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        try {
            GoodsService goodsService = new GoodsServiceImpl();
            List<User> userList = goodsService.selectinvalidUser();
            String invalidUser = JSON.toJSONString(userList);
            // 将 JSON 字符串写回客户端
            response.getWriter().write(invalidUser);
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查看失效会员信息失败失败：" + e.getMessage());
            return "/message.jsp";
        }
    }
    //获取所有商品

    public String showAllGoods(HttpServletRequest request, HttpServletResponse response) throws Exception {
        GoodsService goodsService=new GoodsServiceImpl();
        List<Goods> goods= goodsService.findAll();

        ShowGoodService goodTypeService=new ShowGoodServiceImpl();
        List<GoodsType> goodsTypes = goodTypeService.findAll();
        request.setAttribute("goodsTypeList",goodsTypes);
        request.setAttribute("goodsList",goods);
        return "/admin/showGoods.jsp";
    }
    //获取所有订单
    public String getOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        try {
            OrderService orderService = new OrderServiceImpl();
            List<Order> order = orderService.selectorder();
            String orderList = JSON.toJSONString(order);
            // 将 JSON 字符串写回客户端
            response.getWriter().write(orderList);
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查看失效会员信息失败失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

        //-----------------------------------------------删除功能----------------------------------------------------------
    //删除会员用户
    public String deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)) {
            request.setAttribute("msg", "id为空");
            return "/message.jsp";
        }
        try {
            UserService userService = new UserServiceImpl();
            userService.remove(Integer.parseInt(id));
            //获取删除后的数据
            GoodsService goodsService = new GoodsServiceImpl();
            List<User> userList1 = goodsService.selectUser();
            String userList = JSON.toJSONString(userList1);
            // 将 JSON 字符串写回客户端
            response.getWriter().write(userList);
            return null;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            request.setAttribute("msg", "删除会员用户失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    //删除失效会员用户
    public String deletinvalidUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)) {
            request.setAttribute("msg", "id为空");
            return "/message.jsp";
        }
        try {
            UserService userService = new UserServiceImpl();
            userService.remove(Integer.parseInt(id));
            //获取删除后的数据
            GoodsService goodsService = new GoodsServiceImpl();
            List<User> userList = goodsService.selectinvalidUser();
            String invalidUser = JSON.toJSONString(userList);
            // 将 JSON 字符串写回客户端
            response.getWriter().write(invalidUser);
            return null;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            request.setAttribute("msg", "删除失效会员失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    public String deleteGoodsType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)) {
            request.setAttribute("msg", "id为空");
            return "/message.jsp";
        }
        try {
            GoodsTypeService goodsTypeService = new GoodsTypeServiceImpl();
            goodsTypeService.remove(Integer.parseInt(id));
            //获取删除后的数据
            GoodsService goodsService = new GoodsServiceImpl();
            List<GoodsType> goodsTypeList1 = goodsService.find();
            //存储数据
            String goodsTypeList = JSON.toJSONString(goodsTypeList1);
            response.getWriter().write(goodsTypeList);
            return null;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            request.setAttribute("msg", "删除商品分类失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    //删除商品
    public String deleteshowGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)) {
            request.setAttribute("msg", "id为空");
            return "/message.jsp";
        }
        try {
            GoodsService goodsService = new GoodsServiceImpl();
            goodsService.remove(Integer.parseInt(id));

            GoodsService goodsService1 = new GoodsServiceImpl();
            List<Goods> goodsList = goodsService1.selectGoods();
            String goods = JSON.toJSONString(goodsList);
            // 将 JSON 字符串写回客户端
            response.getWriter().write(goods);
            return null;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            request.setAttribute("msg", "删除商品失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    //-----------------------------------------------------查询模块-------------------------------------------------------
    public String searchUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        String username = request.getParameter("username");
        String gender = request.getParameter("gender");

        try {
            ShowSelectService showSelectService = new ShowSelectServiceimpl();
            List<User> userList = showSelectService.selectUser(username, gender);
            String user = JSON.toJSONString(userList);
            // 将 JSON 字符串写回客户端
            response.getWriter().write(user);
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询会员用户失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    public String searchinvalidUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        String username = request.getParameter("username");
        String gender = request.getParameter("gender");

        try {
            ShowSelectService showSelectService = new ShowSelectServiceimpl();
            List<User> userList = showSelectService.selectinvalidUser(username, gender);
            String user = JSON.toJSONString(userList);
            // 将 JSON 字符串写回客户端
            response.getWriter().write(user);
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询非会员会员用户失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    public String searchGoodsype(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        String level = request.getParameter("level");
        String name = request.getParameter("name");

        try {
            ShowSelectService showSelectService = new ShowSelectServiceimpl();
            List<GoodsType> goodsTypeList = showSelectService.selectGoodsType(name, level);
            String goostype = JSON.toJSONString(goodsTypeList);
            // 将 JSON 字符串写回客户端
            response.getWriter().write(goostype);
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询非会员会员用户失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    //查询商品信息
    public String searchGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
        StringBuilder where1=new StringBuilder(" where 1=1 ");
        String name = request.getParameter("name");
        String pubdate = request.getParameter("pubdate");
        List<Object> I=new ArrayList();
        if (!StringUtils.isEmpty(name))
        {
            where1.append(" and name like ? ");
            I.add("%"+ name +"%");
        }
        if (!StringUtils.isEmpty(pubdate))
        {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date parse = sdf.parse(pubdate);
            where1.append(" and pubdate = ? ");
            I.add(parse);
        }
        ShowGoodService showGoodService = new ShowGoodServiceImpl();
        List<Goods> goodsList= showGoodService.selectByUAD(where1,I);
        request.setAttribute("goodsList",goodsList);
        GoodsTypeService goodTypeService=new GoodsTypeServiceImpl();
        List<GoodsType> goodsTypes = goodTypeService.findAll();
        request.setAttribute("goodsTypeList",goodsTypes);
        return "/admin/showGoods.jsp";
    }

    //查询订单信息
    public String searchorder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
        response.setContentType("application/json;charset=utf-8");
        String status = request.getParameter("status");
        String username = request.getParameter("username");

        try {
            ShowSelectService showSelectService = new ShowSelectServiceimpl();
            List<Order> orderList = showSelectService.selectOrderByName(username, status);
            String order= JSON.toJSONString(orderList);
            // 将 JSON 字符串写回客户端
            response.getWriter().write(order);
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询非会员会员用户失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    //-----------------------------------修改商品信息详情-----------------------------------------------
    public String updateGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String price = request.getParameter("price");
        String goodsType = request.getParameter("goodsType");
        String intro = request.getParameter("intro");
        String star=request.getParameter("star");
        Part picture = request.getPart("picture");
        String dis = picture.getHeader("Content-Disposition");
        String filename = dis.substring(dis.lastIndexOf("filename=") + 10, dis.length() - 1);
        filename = filename.substring(filename.lastIndexOf("\\") + 1);
        String uuidFileName=null;
        if (!filename.equals("")) {//文件名不为空上传图片
            //保存图片
            String basePath = this.getServletContext().getRealPath("WEB-INF" + File.separator + "images");
            File dir=new File(basePath);
            if (!dir.exists()) {
                dir.mkdirs();//创建多级目录
            }
            //获取后缀
            String ext=filename.substring(filename.lastIndexOf(".")+1);
            uuidFileName= UUID.randomUUID().toString().replace("-","");
            uuidFileName=uuidFileName+"."+ext;
            picture.write(basePath+File.separator+uuidFileName);
            picture.delete();
            System.out.println("保存图片成功");
        }
        Goods goods=new Goods(Integer.parseInt(id),name,new Date(),uuidFileName,new BigDecimal(price),Integer.parseInt(star),intro,Integer.parseInt(goodsType));
        GoodsService goodsService=new GoodsServiceImpl();
        goodsService.update(goods);
        List<Goods> goods1= goodsService.findAll();
        request.setAttribute("goodsList",goods1);
        return "/admin/showGoods.jsp";
    }
}